
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class Run {

    public static void main(String[] args) throws IOException {
//      SIMULAÇÕES EXECUTADAS 10 VEZES
//        int runs = 10;
//        gerar_resultados(runs);        
        rodar2(0.8f, 50, 200, 1000, 1, -100f, 100f);
//        gerar_resultados2(0);

    }

    public static void gerar_resultados(int runs) throws IOException {
        PSO pso;
        for (int i = 8; i <= 8; i++) {
            switch (i) {
                case 1: {
                    rodar(i, runs, 0.1f, 0.7f, 50, 200, 1000, 1, -100f, 100f);
//                    pso = new PSO(0.7f, 50, 200, 1000, 1, -100f, 100f);
                    break;
                }
                case 2: {
                    rodar(i, runs, 0.2f, 0.5f, 150, 200, 2500, 1, -100f, 100f);
//                    pso = new PSO(0.5f, 150, 200, 2500, 1, -100f, 100f);
                    break;
                }
                case 3: {
                    rodar(i, runs, 0.1f, 0.6f, 30, 200, 3000, 2, -100f, 100f);
//                    pso = new PSO(0.6f, 30, 200, 3000, 2, -100f, 100f);
                    break;
                }
                case 4: {
                    rodar(i, runs, 0.1f, 0.6f, 50, 200, 1000, 3, -32f, 32f);
//                    pso = new PSO(0.6f, 50, 200, 1000, 3, -32f, 32f);
                    break;
                }
                case 5: {
                    rodar(i, runs, 0.1f, 0.6f, 50, 200, 1000, 4, -600f, 600f);
//                    pso = new PSO(0.6f, 50, 200, 1000, 4, -600f, 600f);
                    break;
                }
                case 6: {
                    rodar(i, runs, 0.2f, 0.5f, 100, 100, 1500, 4, -600f, 600f);
//                    pso = new PSO(0.5f, 100, 100, 1500, 4, -600f, 600f);
                    break;
                }
                case 7: {
                    rodar(i, runs, 0.1f, 0.6f, 30, 200, 1000, 5, -50f, 50f);
//                    pso = new PSO(0.6f, 30, 200, 1000, 5, -50f, 50f);
                    break;
                }
                case 8: {
                    rodar(i, runs, 0.1f, 0.6f, 50, 100, 1000, 6, -50f, 50f);
//                    pso = new PSO(0.6f, 50, 100, 1000, 6, -50f, 50f);
                    break;
                }
            }
        }
    }

    public static void gerar_resultados2(int runs) throws IOException {
        for (int i = 1; i <= 8; i++) {
            switch (i) {
                case 1: {
                    rodar2(0.3f, 50, 200, 1000, 1, -100f, 100f);
//                    pso = new PSO(0.7f, 50, 200, 1000, 1, -100f, 100f);
                    break;
                }
                case 2: {
                    rodar2(0.3f, 150, 200, 2500, 1, -100f, 100f);
//                    pso = new PSO(0.5f, 150, 200, 2500, 1, -100f, 100f);
                    break;
                }
                case 3: {
                    rodar2(0.3f, 30, 200, 3000, 2, -100f, 100f);
//                    pso = new PSO(0.6f, 30, 200, 3000, 2, -100f, 100f);
                    break;
                }
                case 4: {
                    rodar2(0.3f, 50, 200, 1000, 3, -32f, 32f);
//                    pso = new PSO(0.6f, 50, 200, 1000, 3, -32f, 32f);
                    break;
                }
                case 5: {
                    rodar2(0.3f, 50, 200, 1000, 4, -600f, 600f);
//                    pso = new PSO(0.6f, 50, 200, 1000, 4, -600f, 600f);
                    break;
                }
                case 6: {
                    rodar2(0.3f, 100, 100, 1500, 4, -600f, 600f);
//                    pso = new PSO(0.5f, 100, 100, 1500, 4, -600f, 600f);
                    break;
                }
                case 7: {
                    rodar2(0.3f, 30, 200, 1000, 5, -50f, 50f);
//                    pso = new PSO(0.6f, 30, 200, 1000, 5, -50f, 50f);
                    break;
                }
                case 8: {
                    rodar2(0.3f, 50, 100, 1000, 6, -50f, 50f);
//                    pso = new PSO(0.6f, 50, 100, 1000, 6, -50f, 50f);
                    break;
                }
            }
        }
    }

    public static void rodar(int experiment, int runs, float wi, float wf, int dimension, int swarm_size, int iterations, int function, float ylow, float yup) throws IOException {
        System.out.println("Executando Experimento " + experiment + " " + new Date().getTime());
        File saida = new File("E:\\Dropbox/UFRGS/Disciplinas/Análise e Projeto de Algoritmos -/Projeto -", "resultados_experimentos_" + "final_2" + ".txt");
//        File saida = new File("E:\\Dropbox/UFRGS/Disciplinas/Análise e Projeto de Algoritmos -/Projeto", "resultados_experimentos_" + experiment + ".txt");
        FileWriter writer = new FileWriter(saida, true);
//        FileWriter writer = new FileWriter(saida, false);
        PrintWriter printer = new PrintWriter(writer);
//        printer.println("Experimento:\t" + experiment);
        for (float j = wi; j <= wf; j += 0.1f) {
//            printer.println("\tw:\t" + j);
            double sum = 0;
            double best = Double.MAX_VALUE;
            double media = 0;
            PSO pso = null;
            for (int i = 0; i < runs; i++) {
                pso = new PSO(j, dimension, swarm_size, iterations, function, ylow, yup);
                pso.optimize();
                sum += pso.gBest_fitness;
                if (best > pso.gBest_fitness) {
                    best = pso.gBest_fitness;
                }
//                printer.println("\tExecução:" + (i + 1) + "\t" + pso.gBest_fitness);
            }
            printer.println("f" + experiment + "\t" + pso.n + "\t" + pso.yup + "\t" + pso.ylow + "\t"
                    + pso.npt + "\t" + pso.niter + "\t" + pso.wi + "\t" + best + "\t" + (sum / runs));
//            printer.println("\tbest:\t" + best);
//            printer.println("\taverage:\t" + (sum / runs));
//            printer.println("\tsum:\t" + sum);
//            printer.print0ln();
//            printer.println();
//            printer.println();
        }
        printer.println();
        printer.flush();
        writer.close();
        System.out.println("    - Experimento " + experiment + " concluido. " + new Date().getTime());

    }

    public static void rodar2(float wi, int dimension, int swarm_size, int iterations, int function, float ylow, float yup) throws IOException {
        System.out.println("Executando Experimento " + function + " " + new Date().getTime());
        PSO pso = null;
        pso = new PSO(wi, dimension, swarm_size, iterations, function, ylow, yup);
        pso.optimize();
        System.out.println("    - Experimento " + function + " concluido. " + new Date().getTime());
    }
}